package de.tum.beilmaier.im.server;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dev.shell.remoteui.RemoteMessageProto.Message.Request;

import de.novanic.eventservice.client.event.domain.Domain;
import de.novanic.eventservice.client.event.domain.DomainFactory;
import de.novanic.eventservice.service.RemoteEventServiceServlet;
import de.tum.beilmaier.im.client.MessagingService;
import de.tum.beilmaier.im.client.event.CloseChatRoomEvent;
import de.tum.beilmaier.im.client.event.ContactListUpdateEvent;
import de.tum.beilmaier.im.client.event.CreateChatRoomEvent;
import de.tum.beilmaier.im.client.event.IncomingMessageEvent;
import de.tum.beilmaier.im.client.event.LoginEvent;
import de.tum.beilmaier.im.client.event.LogoutEvent;
import de.tum.beilmaier.im.server.dao.impl.ChatRoomDaoImpl;
import de.tum.beilmaier.im.server.dao.impl.UserDaoImpl;
import de.tum.beilmaier.im.shared.model.ChatRoom;
import de.tum.beilmaier.im.shared.model.InstantMessage;
import de.tum.beilmaier.im.shared.model.User;

public class MessagingServiceImpl extends RemoteEventServiceServlet implements MessagingService {

	
	private static UserDaoImpl userDao = null;
	private static ChatRoomDaoImpl chatRoomDao = null;
	private static final long serialVersionUID = 1L;
	
	public MessagingServiceImpl() {
		System.out.println("Server constructor call: " + this.toString());
	}

	
	@Override
	public void initDaos()
	{
		userDao = UserDaoImpl.getInstance();
		chatRoomDao = ChatRoomDaoImpl.getInstance();
		userDao.setContextPath(getServletContext().getRealPath("/"));	
	}

	@Override
	public void sendLoginRequest(Domain UUID, String username, String password) {
		System.out.println("Server - sendLoginRequest" + UUID.getName());
		User login_user = userDao.getUserbyIdAndPassword(username, password);

		LoginEvent loginEvent = new LoginEvent();

		if (login_user != null) {
			System.out.println(login_user.getName());
			loginEvent.setUser(login_user);
		}
		this.addEvent(UUID, loginEvent);

	}

	@Override
	public void updateContactList(Domain USER_DOMAIN, User triggerUser) {

		System.out.println("Server - update Contact List triggered by:  " + triggerUser.getName());
		LinkedHashMap<String, User> loginusers = userDao.getLoggedInUser();
		ContactListUpdateEvent updateevent = new ContactListUpdateEvent();
		updateevent.setLoginusers(loginusers);

		Collection<User> iterate_user = loginusers.values();
		for (User user : iterate_user) {
			this.addEvent(user.getUserDomain(), updateevent);
		}

	}

	@Override
	public void sendLogoutRequest(Domain USER_UUID, User logoutUser) {
		System.out.println("Server - Received user Logout for User " + USER_UUID.getName());
		LogoutEvent logoutEvent = new LogoutEvent();
		logoutEvent.setUser(logoutUser);
		this.addEvent(USER_UUID, logoutEvent);

		LinkedHashMap<String, User> loginusers = userDao.getLoggedInUser();
		ContactListUpdateEvent updateevent = new ContactListUpdateEvent();
		updateevent.setLoginusers(loginusers);

		

		Collection<ChatRoom> roomsToLogoutFrom = chatRoomDao.getChatRoomsByUser(logoutUser);

		for (ChatRoom room : roomsToLogoutFrom) {
			System.out.println("Server -Send ChatRoomLogOutRequest to ChatRooms:" + room.getUUID());
			CloseChatRoomEvent closeChatRoomEvent = new CloseChatRoomEvent();
			closeChatRoomEvent.setChatRoom(room);
			this.addEvent(room.getUUID(), closeChatRoomEvent);
			chatRoomDao.removeChatRoom(room);
		}

		Collection<User> iterate_user = loginusers.values();
		for (User user : iterate_user) {
			System.out.println("Server -Send ContactListUpdate to User:" + user.getName());
			this.addEvent(user.getUserDomain(), updateevent);
		}
		
		userDao.logoutUser(logoutUser);
		
		
	}

	@Override
	public void createChatRoom(Domain USER_UUID, User user1, User user2) {
		System.out.println("Server = Received Chatroom create request from" + user1.getName());
		ChatRoom return_room = chatRoomDao.createChatRoom(user1, user2);
		System.out.println("Server: Following chatchannel has been created " + return_room.getRoomId() + "containing " + return_room.getUsersInRoom().size() + " Users.");

		CreateChatRoomEvent createChatRoomEvent = new CreateChatRoomEvent();
		createChatRoomEvent.setChatRoom(return_room);
		System.out.println("Server: - Send CreateChatRoomResponse to User:" + user1.getName());
		System.out.println("Server: - Send CreateChatRoomResponse to User:" + user2.getName());
		this.addEvent(user1.getUserDomain(), createChatRoomEvent);
		this.addEvent(user2.getUserDomain(), createChatRoomEvent);

	}

	@Override
	public void sendInstantMessagetoChatRoom(InstantMessage im) {
		System.out.println("Server - PostMessageRequest - received from" + im.getSendUser().getName());
		System.out.println("Send to following ChatRoom ID: " + im.getChatRoom().getUUID().toString());
		IncomingMessageEvent messageEvent = new IncomingMessageEvent();
		messageEvent.setInstantMessage(im);
		this.addEvent(im.getChatRoom().getUUID(), messageEvent);

	}

	@Override
	public void sendCreateAccountRequest(Domain dOMAIN_UUID, String text, String text2) {
		
		userDao.addUser(new User(text, text2, text, text));
		
	}

}
